package controllers

import (
	"errors"
	"news/common/response"
	"news/constStatus"
	"news/logic"
	"news/models"

	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
)

//创建管理员
func CreateAdminHandler(c *gin.Context) {
	p := new(models.UserAdmin)
	if err := c.ShouldBindJSON(p); err != nil {
		zap.L().Error("CreateAdmin with invalid param", zap.Error(err))
		errors, ok := err.(validator.ValidationErrors)
		if !ok {
			response.ResponseErrorWithMsg(c, response.CodeInvalidParam, err.Error())
			return
		}
		response.ResponseErrorWithMsg(c, response.CodeInvalidParam, removeTopStruct(errors.Translate(trans)))
		return
	}

	err := logic.CreateAdminUser(p)
	if err != nil {
		zap.L().Error("CreateAdminUser failed", zap.Error(err))
		if errors.Is(err, constStatus.ErrorUserExist) {
			response.ResponseErr(c, response.CodeUserExist)
			return
		}
		response.ResponseErr(c, response.CodeServerBusy)
		return
	}
	response.ResponseSuccess(c, nil)

}
